﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace MadLibrary.Net.PeerToPeer
{
    /// <summary>
    /// Holds peerer informations.
    /// </summary>
    public class PeererInfo
    {
        /// <summary>
        /// Gets or sets the listening port.
        /// </summary>
        /// <value>
        /// The listening port.
        /// </value>
        public int ListeningPort { get; set; }

        /// <summary>
        /// Gets or sets the target IP.
        /// </summary>
        /// <value>
        /// The target IP.
        /// </value>
        public IPAddress TargetIP { get; set; }

        /// <summary>
        /// Gets or sets the communication start.
        /// </summary>
        /// <value>
        /// The communication start.
        /// </value>
        public DateTime CommunicationStart { get; set; }

        /// <summary>
        /// Gets or sets the communication end.
        /// </summary>
        /// <value>
        /// The communication end.
        /// </value>
        public DateTime? CommunicationEnd { get; set; }

        /// <summary>
        /// Gets the target of peerer.
        /// </summary>
        public IPEndPoint Target 
        { 
            get 
            {
                return new IPEndPoint(this.TargetIP, this.ListeningPort);
            } 
        }

        /// <summary>
        /// Gets or sets the encoding.
        /// </summary>
        /// <value>
        /// The encoding.
        /// </value>
        public Encoding Encoding { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="PeererInfo"/> class.
        /// </summary>
        public PeererInfo()
        {
            this.ListeningPort = 0;
            this.TargetIP = IPAddress.Loopback;
            this.CommunicationStart = DateTime.Now;
            this.CommunicationEnd = null;
            this.Encoding = Encoding.ASCII;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PeererInfo"/> class.
        /// </summary>
        /// <param name="ListeningPort">The listening port.</param>
        /// <param name="Address">The address.</param>
        public PeererInfo(int ListeningPort, IPAddress Address)
        {
            this.ListeningPort = ListeningPort;
            this.TargetIP = Address;
            this.CommunicationStart = DateTime.Now;
            this.CommunicationEnd = null;
            this.Encoding = Encoding.ASCII;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PeererInfo"/> class.
        /// </summary>
        /// <param name="ListeningPort">The listening port.</param>
        /// <param name="Address">The address.</param>
        /// <param name="Encoding">The encoding.</param>
        public PeererInfo(int ListeningPort, IPAddress Address, Encoding Encoding)
        {
            this.ListeningPort = ListeningPort;
            this.TargetIP = Address;
            this.CommunicationStart = DateTime.Now;
            this.CommunicationEnd = null;
            this.Encoding = Encoding;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PeererInfo"/> class.
        /// </summary>
        /// <param name="ListeningPort">The listening port.</param>
        /// <param name="Address">The address.</param>
        /// <param name="CommunicationStart">The communication start.</param>
        /// <param name="CommunicationEnd">The communication end.</param>
        /// <param name="Encoding">The encoding.</param>
        public PeererInfo(int ListeningPort, IPAddress Address, DateTime CommunicationStart, DateTime? CommunicationEnd, Encoding Encoding)
        {
            this.ListeningPort = ListeningPort;
            this.TargetIP = Address;
            this.CommunicationStart = CommunicationStart;
            this.CommunicationEnd = CommunicationEnd;
            this.Encoding = Encoding;
        }

        /// <summary>
        /// Starts the communication and sets competent properties to thier values.
        /// </summary>
        public void StartCommunication()
        {
            this.CommunicationStart = DateTime.Now;
        }

        /// <summary>
        /// Ends the communication and sets competent properties to thier values.
        /// </summary>
        public void EndCommunication() 
        {
            this.CommunicationEnd = DateTime.Now;
        }

        /// <summary>
        /// Destructor
        /// </summary>
        ~PeererInfo()
        {
            this.EndCommunication();
        }
    }
}
